class Main extends React.Component {
    constructor(props) {
  super(props);
  this.state = {
     list:[
       
    ]
  };
}

formatlist(list) {
console.log(list,'list');

var that = this;
var listitem = [];
list.map((x,i) => {
    var state = '上架';
    console.log(x.state);
    if(x.state==1)state='下架';
    if(x.state==2)state='售空';
    var stateclass = 'state s_'+x.state,kc=0,sizearr=[];
    sizearr=x.inventory.split(',');
        sizearr.forEach(x=>{ kc+=Number(x) })
    //<button onClick={that.edit.bind(that,x.id,2)} className='btn btn-danger center'>删除</button>
    listitem.push(
        <div key={i}  className='item panel-body'>
                            <img src={x.thumbPicPath}/>
                            <div className='word'>
                                <div className='top'>
                                    <p className='price'>积分:{x.score}</p>
                                    <p className='name'>{x.name}</p>
                                </div>
                                <div className='bottom'>
                                    <p className='Inventory'>库存:{kc}</p>
                                    <p className={stateclass}>状态:{state}</p>
                                </div>
                                <div className='btns'>
                                        <button onClick={that.edit.bind(that,x.id,0)} className='btn btn-primary center'>编辑</button> 
                                        <button style={{display:x.state!=2?"":"none"}} onClick={that.edit.bind(that,x.id,1)} className='btn btn-default center'>{x.state==0?"下架":"上架"}</button> 
                                        <button style={{display:x.state==2?"":"none"}} className='btn btn-default center'>售空</button> 
                                          
                                </div>
                                
                            </div>
                        </div>
    )
  });
  return listitem;
  
}

edit(e,i){
  console.log(e,'e');
  console.log(i,'i');
  var that = this;
  if(i==0){ //编辑
    window.location.href='./scoregoods.html?id='+e
  }else if(i==1){//上架
    setscoregoodsstate({id:e},res=>{
        console.log(res);
        if(res.issuccess==1){
            if(res.state=='0'){
                alert('上架成功')
            }else{
                alert('下架成功')
            }
            var list = that.state.list;
            
            var newlist = [];
            list.forEach(e => {
                var item = e;
                item.state = res.state;
                newlist.push(item)
            });
            console.log(newlist);
            
            var listitem = this.formatlist(newlist)
            that.setState({
                list:newlist,
                listitem 
            })
        }

    })
  }else{

  }
  

}
getnew(){
  window.location.href='./scoregoods.html?isnew=0'
}



componentDidMount() {
    getsscoregoodsblist(res=>{
        console.log(res);
        if(res.issuccess==1){
           var listitem = this.formatlist(res.list);
           this.setState({
    listitem,
    list:res.list
  })
        }
        
    })
    
    // getcategorylist(res=>{
    //     if(res.issuccess==1){
    //         $.cookie('categorylist',JSON.stringify(res.list))
    //     }
    // })




}
     render() {
         // 
       var listitem = this.formatlist(this.state.list);

            return(
                <div className='content_wrap panel-default content_wrap'>
                    <div className='breadLine '> 
                        当前位置 > 积分商品管理
                        </div>
                    <div className='btns'>
                        <button className='btn add btn-primary center ' onClick={this.getnew}  id='btnimg_add'> <img src={Constant.ADD}/> 添加商品</button>
                    </div>

                    <div className='goods panel-body'>
                        {listitem}
                    </div>
                 
                </div>
            )
        }
    }





ReactDOM.render(
    <div>
        <TopLine />
        <div id='body'>
            {/* <LeftMenu code='7'/> */}
            <LeftMenu code='scoremain'/>
            <Main/>
        </div>
        </div>
            // <Barrage/>
,$('.container')[0]
)